//测试手机微信小程序

//开发环境

// #ifdef H5
// var baseURL = 'http://localhost:3333/'
var baseURL = 'http://192.168.145.227:3333/'
// #endif

// #ifdef APP-PLUS
// var baseURL = 'http://localhost:3333/'
var baseURL = 'http://192.168.145.227:3333/'
// #endif

// #ifdef MP-WEIXIN
// var baseURL = 'http://localhost:3333/'
var baseURL = 'http://192.168.145.227:3333/'
// #endif

const httpInterceptor = {
  invoke(options) {
    if (!options.url.startsWith('http')) {
      options.url = baseURL + options.url
    }
    options.timeout = 10000
    // options.header = {
    //   'source-client': 'miniapp',
    // }
    // const memberStore = useMemberStore()
    // const token = memberStore.profile?.token
    // if (token) {
    //   options.header.Authorization = token
    // }
    // options.header("content-type", "application/json")
    console.log(options)
  },
}

uni.addInterceptor('request', httpInterceptor)
uni.addInterceptor('uploadFile', httpInterceptor)


export const http = (options) => {
  return new Promise((resolve, reject) => {
    uni.request({
      ...options,
      success(res) {
        
        if (res.statusCode >= 200 && res.statusCode < 300) {
          resolve(res.data)
        } else if (res.statusCode === 401) {
          // const memberStore = useMemberStore()
          // memberStore.clearProfile()
          // uni.navigateTo({ url: '/pages/login/login' })
          reject(res)
        } else {
          uni.showToast({
            icon: 'none',
            title: res.data.error || '请求错误',
          })
          reject(res)
        }
      },
      fail(error) {
        uni.showToast({
          icon: 'none',
          title: '网络不好',
        })
        reject(error)
      },
    })
  })
}
